package com.symaster.common.util;

import java.util.Random;

/**
 * @author yinmiao
 * @date 2021-08-03 14:53
 */
public class ProbabilityUtil {

    private static final Random random = new Random();

    /**
     * 概率发生
     */
    public static boolean exeProbability(double py, double max) {
        if (py >= max) return true;
        int c = 1000;
        double m00 = max - py;
        double m01 = m00 / max;
        double m02 = m01 * c;
        double i = random.nextInt(c);
        return !(i <= m02);
    }

    /**
     * 概率发生
     * @param py (0-1) 表示多大概率返回True
     */
    public static boolean exeProbability(double py) {
        return exeProbability(py, 1);
    }
}
